package com.tianfeng.news.ui.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.tianfeng.news.R;
import com.tianfeng.news.globle.ConstantUrl;
import com.tianfeng.news.net.HttpView;
import com.tianfeng.news.net.OkHttpHelper;
import com.tianfeng.news.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Data:  2019/6/21
 * Auther: xcd
 * Description:
 */
public class InsertUserWithDialogFragment extends DialogFragment
        implements View.OnClickListener , HttpView {

    @BindView(R.id.et_account)
    EditText et_account;
    @BindView(R.id.et_name)
    EditText et_name;

    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_nick)
    EditText et_nick;

    @BindView(R.id.tv_cancel)
    TextView tv_cancel;
    @BindView(R.id.tv_ok)
    TextView tv_ok;

    String userId;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_fragment_nsertuserwith, container);

        et_account = view.findViewById(R.id.et_account);
        et_name = view.findViewById(R.id.et_name);
        et_phone = view.findViewById(R.id.et_phone);
        et_nick = view.findViewById(R.id.et_nick);


        view.findViewById(R.id.tv_ok).setOnClickListener(this);
        view.findViewById(R.id.tv_cancel).setOnClickListener(this);;


        Bundle bundle = getArguments();
        if (bundle != null) {
            userId = bundle.getString("userId");
        }

        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.dimAmount = 0.5f;
        windowParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(windowParams);
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            window.setLayout((int) (dm.widthPixels * 0.75), -2);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_ok:
                String accwithnum = et_account.getText().toString().trim();
                if (TextUtils.isEmpty(accwithnum)){
                    ToastUtil.showToast( "支付宝账号不能为空！");
                    return;
                }
                String accname = et_name.getText().toString().trim();
                if (TextUtils.isEmpty(accwithnum)){
                    ToastUtil.showToast( "真实姓名不能为空！");
                    return;
                }
                String phone = et_phone.getText().toString().trim();
                if (TextUtils.isEmpty(accwithnum)){
                    ToastUtil.showToast( "绑定手机号不能为空！");
                    return;
                }
                String usernick = et_nick.getText().toString().trim();
                if (TextUtils.isEmpty(accwithnum)){
                    ToastUtil.showToast( "用户昵称不能为空！");
                    return;
                }

                HashMap<String, String> params = new HashMap<>();
                params.put("id", userId);
                params.put("accwithnum", accwithnum);//提现账户数字
                params.put("accname", accname);//提现账户真是名字
                params.put("phone", phone);//账户绑定手机号
                params.put("usernick", usernick);//账户用户昵称
                OkHttpHelper.getAsyncHttp(getActivity(), 10001, params,
                        ConstantUrl.REQUEST_INSERTUSERWITH, this);
                break;

        }
    }


    @Override
    public void onSuccessResult(int requestCode, int returnCode, String returnMsg, String returnData, Map<String, String> paramsMaps) {
        if (returnMsg.indexOf("提现成功")!=-1){
            dismiss();
            getActivity().finish();
        }else {
            ToastUtil.showToast( returnMsg);
        }
    }

    @Override
    public void onFailure(int callbackId, String data) {

    }
}
